//
//  QQUserInfo.m
//  QQZF
//
//  Created by hdz on 2018/5/11.
//  Copyright © 2018年 com.szqqkj. All rights reserved.
//

#import "QQUserInfo.h"

static QQUserInfo *_userinfo = nil;
static NSString *key = @"QQUserAccount";
static dispatch_once_t onceToken;

@implementation QQUserInfo

- (void)saveUserAccount {
    @synchronized (self) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:self.mj_keyValues forKey:key];
        [defaults synchronize];
    }
}

+ (instancetype)readUserAccount {
    @synchronized (self) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        _userinfo = [QQUserInfo mj_objectWithKeyValues:[defaults objectForKey:key]];
        [_userinfo saveUserAccount];
        return _userinfo;
    }
}

- (void)removeUserAccount {
    @synchronized (self) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults removeObjectForKey:key];
    }
}

+ (instancetype)shareUserInfo {
    //如果保存过，则返回User对象；否则返回nil
    if (![[NSUserDefaults standardUserDefaults] valueForKey:key]) {
        return nil;
    }
    dispatch_once(&onceToken, ^{
        _userinfo = [self readUserAccount];
    });
    return _userinfo ;
}

#pragma mark 销毁单例
+ (void)destroy {
    onceToken = 0l;
    _userinfo = nil;
}


#pragma mark - 退出登录，移除数据
+ (void)logout {
    [[QQUserInfo shareUserInfo] removeUserAccount];
    [QQUserInfo destroy];
}


- (void)setStatus:(NSInteger)status {
    _status = status;
    _statsColor = @"#999999";
    if (status == 1) {
        _statusStr = @"";//已实名
    }else if (status == 2){
        _statusStr = @"禁用";
    }else if (status == 3){
        _statusStr = @"锁定";
    }else if (status == 4){
        _statusStr = @"未实名";
    }else if (status == 5){
        _statusStr = @"认证中";
        _statsColor = @"#3E4675";
    }else{
        _statusStr = @"认证失败";
        _statsColor = @"#DC143C";
    }
}

@end
